package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import connection.MarketPlaceServer;



public class CheckLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private javax.jms.Connection connection;
	private Session session1;
	private Topic marketTopic;
	private MessageConsumer consumer;
	private Topic replyTopic;

	private Queue marketQueue;
	private MessageConsumer consumerQ;
	private Queue replyQueue;
	
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	    Properties properties = new Properties();
	    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
	    properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
	    properties.put(Context.PROVIDER_URL, "localhost");
		
		InitialContext jndi;
		try {
			jndi = new InitialContext(properties);
			ConnectionFactory conFactory = (ConnectionFactory)jndi.lookup("XAConnectionFactory");
			connection = conFactory.createConnection();
			
			session1 = connection.createSession( false, Session.AUTO_ACKNOWLEDGE );
			marketTopic = (Topic)jndi.lookup("MarketTopic");
			marketQueue = (Queue) jndi.lookup("queue/MarketQueue");
			connection.start();
		}
		catch (Exception e)
		{
			
		}
	}
    
    public CheckLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		   String userName = request.getParameter("username"); 
		   String password = request.getParameter("password");
		   int returnVal = 0;
		   HttpSession session = request.getSession();
		   session.setAttribute("activeUser", userName);
	
		   session.setAttribute("userFName", new MarketPlaceServer().fetchUserFName(userName));
		   session.setAttribute("userLastTime", new MarketPlaceServer().fetchUserLastLoginTime(userName));
		   
		   String result = new String();
		   try
		   {
     	   MessageProducer MP = session1.createProducer(marketQueue);
		   TextMessage TM = session1.createTextMessage("signUp"+"$"+userName+"$"+password);
	
		    replyQueue = session1.createTemporaryQueue();
			consumerQ = session1.createConsumer(replyQueue);
	
			TM.setJMSReplyTo(replyQueue);
			MP.send(TM);
			TextMessage Reply = (TextMessage) consumerQ.receive();
			result = (Reply.getText());
            returnVal = Integer.parseInt(result);
		   }
		   catch (Exception e)
		   {
			   request.setAttribute("Error", "Exception occured in the " +
  						"system. Please try again later.");
			   returnVal = 1;
		   }
		   if (returnVal == 0)
		   {
			   // Successful sign in
			   getServletContext().
			   getRequestDispatcher("/sampleMarketPlaceProxy/Welcome.jsp").
			   forward(request, response);
		   }
		   else if (returnVal == 1)
		   {
			   request.setAttribute("Error", "Exception occured in the " +
			   						"system. Please try again later.");
			   getServletContext().
			   getRequestDispatcher("/sampleMarketPlaceProxy/loginException.jsp").
			   forward(request, response);
		   }
		   else
		   {
			   request.setAttribute("Error", "Exception occured in the " +
  						"system. Please try again later.");
			   getServletContext().getRequestDispatcher(
				"/sampleMarketPlaceProxy/loginError.jsp").forward(request, response);
		   }
	}
}
